$(function () {
    $("#jqGrid").Grid({
        url: '../goods/list',
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '商品类型', name: 'categoryName', index: 'category_id', width: 60},
            {label: '名称', name: 'name', index: 'name', width: 60},
            {label: '原价', name: 'marketPrice', index: 'market_price', width: 80},
            {label: '当前折扣', name: 'discount', index: 'discount', width: 80},
            {label: '当前折后价格', name: 'retailPrice', index: 'retail_price', width: 80}
           ]
    });
    function operateTrunk(el, options, rowData) {
        if (rowData.id !="default") {
            return "<input type='text'>"
        }
        var columnArray = jQuery("#jqGrid").jqGrid('名称','name');
        console.log(columnArray);
        return"";
    }
});

var ztree;

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};
var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        uploadList: [],
        imgName: '',
        visible: false,
        goods: {
            id:0,
            retailPrice: 0,
            marketPrice: 0,
            discount:1.00,
            categoryName: '',
            name:''
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        }

    },
    methods: {

        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../goods/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.goods = r.goods;
                    vm.goods.isSecKill = r.goods.isSecKill+"";
                    $('#goodsDesc').editable('setHTML', vm.goods.goodsDesc.replace("1我2", " "));
                    console.log(vm.goods.goodsDesc.replace("1我2", " "));
                    vm.getCategory();
                }
            });
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.uploadList = [];
            vm.getInfo(id);
            vm.getBrands();
            vm.getMacro();
            vm.getAttributeCategories();
        },
        handleSubmit: function (name) {
            vm.goodsDescHtml = $('#goodsDesc').editable('getHTML');
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        saveOrUpdate: function (event) {
            var url = vm.goods.id == null ? "../goods/save" : "../goods/update";
            vm.goods.goodsDesc = $('#goodsDesc').editable('getHTML');
            vm.goods.goodsImgList = vm.uploadList;

            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.goods),
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        handleSuccess(res, file) {
            // 因为上传过程为实例，这里模拟添加 url
            file.imgUrl = res.url;
            file.name = res.url;
            vm.uploadList.add(file);
        }
    },
    mounted() {

        this.uploadList = this.$refs.upload.fileList;

    }
});
